<?php 
class Database_manage extends MY_Controller {
	function __construct() {
		parent::__construct();
	}
	
	function main() {
		if ($_SESSION['cid'] != ADMIN_SUPER_CID) {
			show_404();
			return FALSE;
		}
		$this->load->database();
		$tables = $this->db->list_tables();
		foreach ($tables as $k => $v) {
			$data['table'][$k]['table_name'] = $v;
			$data['table'][$k]['table_rows'] = $this->db->count_all($v);
		}
		
		$data['database_info'] = $this->db->platform() . " " . $this->db->version();
		
		$this->load->view("admin/database_manage/table_list", $data);
	}
	
	function table_optimize() {
		$this->load->dbutil();
		$table_name = $this->input->post("table_name");
        if ($this->dbutil->optimize_table($table_name)) {
		    $this->output->set_output(json_encode('optimize_succeeded'));
	        return ;
		} else {
			$this->output->set_output(json_encode('optimize_failed'));
	        return ;
		}
	}
	
	function table_repair() {
		$this->load->dbutil();
		$table_name = $this->input->post("table_name");
        if ($this->dbutil->repair_table($table_name)) {
		    $this->output->set_output(json_encode('repair_succeeded'));
	        return ;
		} else {
			$this->output->set_output(json_encode('repair_failed'));
	        return ;
		}
	}
	
	function database_backup() {
		$this->load->dbutil();
		$prefs['format'] = 'txt';
		$backup =& $this->dbutil->backup($prefs);
		
		$this->load->helper('file');
		$backup_path = DATABASE_BACKUP_FOLDER . date("Y-m-d_H-i-s") . "_backup.sql";
		if (write_file($backup_path, $backup)) {
			$this->output->set_output(json_encode('backup_succeeded'));
	        return ;
		} else {
			$this->output->set_output(json_encode('backup_failed'));
	        return ;
		}
	}
	
	function restore_list() {
		$this->load->helper('file');
		$this->load->helper('number');
		$backup_folder = DATABASE_BACKUP_FOLDER;
		$temp_data = get_dir_file_info($backup_folder, $top_level_only = TRUE);
		
		if (!empty($temp_data)) {
			$k = 0;
			foreach ($temp_data as $v) {
				$data[$k]['name'] = str_ireplace(".sql", "", $v['name']);
				$data[$k]['size'] = byte_format($v['size']);
				$data[$k]['date'] = date("Y-m-d H:i", $v['date']);
				$k++;
			}
			
			$temp['data'] = $data;
		} else {
			$temp = '';
		}
		
		$this->load->view('admin/database_manage/restore_list', $temp);
	}
	
	function backupdata_download() {
		$this->load->helper('download');
		$backupdata = $this->input->post("backupdata") . ".sql";
        $data = file_get_contents(DATABASE_BACKUP_FOLDER . $backupdata);
		force_download($backupdata, $data);
		//$this->output->set_output(json_encode("ok"));
	}
	
	function backupdata_delete() {
		$backupdata = $this->input->post("backupdata") . ".sql";
		if (!unlink(DATABASE_BACKUP_FOLDER . $backupdata)) {
			$this->output->set_output(json_encode("delete_failed"));
		} else {
			$this->output->set_output(json_encode("delete_succeeded"));
		}
	}
}